package com.repair.controller;

import com.repair.entity.User;
import com.repair.entity.RepairOrder;
import com.repair.entity.RepairTrack;
import com.repair.service.UserService;
import com.repair.service.RepairOrderService;
import com.repair.service.RepairTrackService;
import com.repair.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/admin")
@PreAuthorize("hasRole('ADMIN')")
public class AdminController {

    @Autowired
    private UserService userService;

    @Autowired
    private RepairOrderService repairOrderService;

    @Autowired
    private RepairTrackService repairTrackService;

    @Autowired
    private AdminService adminService;

    @GetMapping("/dashboard")
    public ResponseEntity<?> getDashboardData() {
        Map<String, Object> data = repairOrderService.getDashboardData();
        return ResponseEntity.ok(data);
    }

    @GetMapping("/users")
    public ResponseEntity<List<User>> getUsers() {
        List<User> users = userService.list();
        return ResponseEntity.ok(users);
    }

    @GetMapping("/repairmen")
    public ResponseEntity<List<User>> getRepairmen() {
        List<User> repairmen = userService.getRepairmen();
        return ResponseEntity.ok(repairmen);
    }

    @GetMapping("/orders")
    public ResponseEntity<?> getOrders() {
        List<Map<String, Object>> orders = repairOrderService.getOrderList();
        return ResponseEntity.ok(orders);
    }

    @GetMapping("/orders/{id}")
    public ResponseEntity<?> getOrderDetails(@PathVariable Long id) {
        Map<String, Object> order = repairOrderService.getOrderDetails(id);
        return ResponseEntity.ok(order);
    }

    @GetMapping("/statistics")
    public ResponseEntity<?> getStatistics() {
        Map<String, Object> statistics = repairOrderService.getStatistics();
        return ResponseEntity.ok(statistics);
    }

    @PutMapping("/users/{id}/status")
    public ResponseEntity<?> toggleUserStatus(@PathVariable Long id) {
        userService.toggleUserStatus(id);
        return ResponseEntity.ok().build();
    }

    @PutMapping("/repairmen/{id}/status")
    public ResponseEntity<?> toggleRepairmanStatus(@PathVariable Long id) {
        userService.toggleRepairmanStatus(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/tracks")
    public ResponseEntity<?> getTracks() {
        List<RepairTrack> tracks = repairTrackService.list();
        return ResponseEntity.ok(tracks);
    }

    @PostMapping("/users")
    public ResponseEntity<?> createUser(@RequestBody User user) {
        userService.register(user);
        return ResponseEntity.ok().build();
    }

    @PutMapping("/users/{userId}")
    public ResponseEntity<Void> updateUser(@PathVariable Long userId, @RequestBody User user) {
        userService.updateProfile(user);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/users/{userId}")
    public ResponseEntity<Void> deleteUser(@PathVariable Long userId) {
        adminService.deleteUser(userId);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/orders/status/{status}")
    public ResponseEntity<List<RepairOrder>> getOrdersByStatus(@PathVariable Integer status) {
        return ResponseEntity.ok(adminService.getOrdersByStatus(status));
    }

    @PutMapping("/orders/{orderId}/status")
    public ResponseEntity<Void> updateOrderStatus(@PathVariable Long orderId, @RequestParam Integer status) {
        adminService.updateOrderStatus(orderId, status);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/orders/{orderId}")
    public ResponseEntity<Void> deleteOrder(@PathVariable Long orderId) {
        adminService.deleteOrder(orderId);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/tracks/{id}")
    public ResponseEntity<?> deleteTrack(@PathVariable Long id) {
        repairTrackService.removeById(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/statistics/orders")
    public ResponseEntity<List<Map<String, Object>>> getOrderStatistics() {
        return ResponseEntity.ok(adminService.getOrderStatistics());
    }

    @GetMapping("/statistics/repairmen")
    public ResponseEntity<List<Map<String, Object>>> getRepairmanStatistics() {
        return ResponseEntity.ok(adminService.getRepairmanStatistics());
    }

    @PostMapping("/login")
    public ResponseEntity<?> login(@RequestBody Map<String, String> loginRequest) {
        try {
            String token = userService.login(loginRequest.get("username"), loginRequest.get("password"));
            Map<String, String> response = new HashMap<>();
            response.put("token", token);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
} 